package com.swx.identityservice.webapis;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swx.identityservice.dtos.transferposition.InsertTransferPositionDto;
import com.swx.identityservice.dtos.transferposition.TransferPositionListOutputDto;
import com.swx.identityservice.dtos.transferposition.TransferPositionOutputDto;
import com.swx.identityservice.dtos.transferposition.UpdateTransferPositionDto;
import com.swx.identityservice.models.ScbpTransferPosition;
import com.swx.identityservice.service.ScbpTransferPositionService;
import com.swx.identityservice.utils.JaxrsResponse;
import com.swx.identityservice.utils.ResponseMessages;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author lxh
 * @version V1.0.0
 * @date 2018-6-11 17:31
 */
@Slf4j
@Api(tags = "调岗指派控制器")
@RestController
public class ScbpTransferPositionController {

    @Autowired
    private ScbpTransferPositionService scbpTransferPositionService;
    private ModelMapper modelMapper;
    public ScbpTransferPositionController() {
        modelMapper = new ModelMapper();
    }

    @ApiOperation("新增调派")
    @PostMapping(value = "/addtransferposition")
    public JaxrsResponse addtransferposition(@RequestBody InsertTransferPositionDto insertTransferPositionDto) {
        ScbpTransferPosition scbpTransferPosition = modelMapper.map(insertTransferPositionDto, ScbpTransferPosition.class);
        scbpTransferPosition.setCreationtime(new Date());
        int sr = scbpTransferPositionService.insertSelective(scbpTransferPosition);
        if (sr > 0) return JaxrsResponse.builder().code(0).status("success").build();
        return JaxrsResponse.builder().code(1).status("fail").message(ResponseMessages.INSERT_FAIL).build();

    }

    @ApiOperation("编辑调派")
    @PostMapping(value = "/edittransferposition")
    public JaxrsResponse edittransferposition(@RequestBody UpdateTransferPositionDto updateTransferPositionDto) {
        ScbpTransferPosition scbpTransferPosition = modelMapper.map(updateTransferPositionDto, ScbpTransferPosition.class);
        scbpTransferPosition.setLastmodificationtime(new Date());
        int sr = scbpTransferPositionService.updateByPrimaryKeySelective(scbpTransferPosition);
        if (sr > 0) return JaxrsResponse.builder().code(0).status("success").build();
        return JaxrsResponse.builder().code(1).status("fail").message(ResponseMessages.INSERT_FAIL).build();
    }

    @ApiOperation("删除调派")
    @DeleteMapping(value = "/deltransferposition")
    public JaxrsResponse delete(@RequestParam int id) {
        ScbpTransferPosition scbpTransferPosition = new ScbpTransferPosition();
        scbpTransferPosition.setId(id);
        scbpTransferPosition.setIspermission(true);
        int sr = scbpTransferPositionService.updateByPrimaryKeySelective(scbpTransferPosition);
        if (sr > 0) return JaxrsResponse.builder().code(0).status("success").data(scbpTransferPosition.getId()).build();
        return JaxrsResponse.builder().code(1).status("fail").message(ResponseMessages.DELETE_FAIL).build();
    }

    @GetMapping(value = "/gettransferposition")
    public JaxrsResponse get(@RequestParam(defaultValue = "1", value = "pageIndex") int pageIndex,
                             @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<ScbpTransferPosition> scbpTransferPosition = scbpTransferPositionService.selectAll();
        PageInfo<ScbpTransferPosition> page = new PageInfo<>(scbpTransferPosition);
        TransferPositionListOutputDto transferPositionListOutputDto = new TransferPositionListOutputDto();
        transferPositionListOutputDto.setTotal(page.getTotal());
        for (int j = 0; j < page.getList().size(); j++) {
            TransferPositionOutputDto transferPositionOutputDto = modelMapper.map(page.getList().get(j), TransferPositionOutputDto.class);
            transferPositionListOutputDto.addUser(transferPositionOutputDto);
        }
        return JaxrsResponse.builder().code(0).status("success").data(transferPositionListOutputDto).build();
    }
}
